package com.zzw.testdemo01.handler;

import com.zzw.testdemo01.IdUtils;
import com.zzw.testdemo01.item.ItemOne;
import net.minecraft.dispenser.Position;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.ai.attributes.Attributes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.BowItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.ListNBT;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.player.ArrowNockEvent;
import net.minecraftforge.event.entity.player.AttackEntityEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.UUID;

@Mod.EventBusSubscriber(modid = IdUtils.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class OneModEventHandler {

    // 注册事件监听器
    public OneModEventHandler() {
        MinecraftForge.EVENT_BUS.register(this);
    }

    // 监听拉弓
    @SubscribeEvent
    public static void onArrowNock(ArrowNockEvent event) {
        PlayerEntity player = event.getPlayer();

        ItemStack heldItem = player.getMainHandItem();

        // 检查玩家是否手持弓箭
        if (heldItem.getItem() instanceof BowItem) {
            //给一个 3s 的 力量 II
            player.addEffect(new EffectInstance(Effects.DAMAGE_BOOST, 20 * 3, 1));
        }
    }

    @SubscribeEvent
    public static void onDeath(ArrowNockEvent event) {
        PlayerEntity player = event.getPlayer();

        ItemStack heldItem = player.getMainHandItem();

        // 检查玩家是否手持弓箭
        if (heldItem.getItem() instanceof BowItem) {
            //给一个 3s 的 力量 II
            player.addEffect(new EffectInstance(Effects.DAMAGE_BOOST, 20 * 3, 1));
        }
    }

    private static Map<UUID, PlayerInventory> playerDeathInventorys = new HashMap<>();
    private static Map<UUID, Integer> playerDeathXps = new HashMap<>();
    private static Map<UUID, Position> playerDeathPositions = new HashMap<>();

    /**
     * 玩家死亡时，保存其背包数据和xp数据
     * @param event
     */
    @SubscribeEvent
    public static void onPlayerDeath(LivingDeathEvent event) {
        Entity entity = event.getEntity();
        if (entity instanceof PlayerEntity) {
            PlayerEntity player = (PlayerEntity) entity;
            // 保存玩家的物品
            PlayerInventory inventory = player.inventory;       //玩家背包
            playerDeathInventorys.put(player.getUUID(), inventory);      //保存玩家背包
            int experienceLevel = player.experienceLevel;       //玩家经验
            playerDeathXps.put(player.getUUID(), experienceLevel);       //保存玩家经验
            Position position = new Position(player.getX(), player.getY(), player.getZ());      //获取玩家当前位置
            playerDeathPositions.put(player.getUUID(), position);       //保存玩家死亡地点
            player.inventory.dropAll();
            player.experienceLevel = 0;
        }
    }


    @SubscribeEvent
    public static void onPlayerClone(PlayerEvent.Clone event) {
//        PlayerEntity originalPlayer = event.getOriginal();
        PlayerEntity newPlayer = event.getPlayer();
        if (playerDeathInventorys.containsKey(newPlayer.getUUID())) {
            ListNBT nbt = new ListNBT();
            playerDeathInventorys.get(newPlayer.getUUID()).save(nbt);
            playerDeathInventorys.remove(newPlayer.getUUID());
            newPlayer.inventory.load(nbt);
            if (playerDeathXps.containsKey(newPlayer.getUUID())){
                newPlayer.experienceLevel = playerDeathXps.get(newPlayer.getUUID());
            }
        }
    }

}
